import ChartBase from "./chart-base";
import {
  grid,
  legend,
  tooltip,
  xAxis,
  yAxis,
  barSeries,
  barSeriesLabel,
} from "./common";
import { deepClone, mergeObject } from "@/utils";

export default class Antv extends ChartBase {
  constructor() {
    super();
  }

  // 基础描述
  _info = {
    name: "蜂窝图",
    type: "antv-g2", // 图表类型

    group: "antv", // 图表分类
    component: "antv-g2",
    icon: "antv-g2", // 图表展示ICON
  };

  // 总 配置项
  _defaultOption = {
    series: [],
    count: [],
  };

  // 数据结构描述
  _dataPoolConfig = [
    {
      label: "类目轴",
      key: "field",
      type: ["string", "date", "number"],
      limit: 1,
    },
    {
      label: "数值",
      key: "value",
      type: ["number"],
      limit: -1,
    },
  ];

  // 样式配置描述信息
  _styleOptionConfig = [];

  getOption({
    dataField = [],
    dataList = [],
    dataTotal = 0,
    dataPoolField,
    styleOptionConfig = [],
    themeColor = [],
    themeStyle = {},
  }) {
    // 更新描述字段
    this._updateDataPoolField(dataField, dataPoolField);

    const { field, value } = dataPoolField;

    let { option } = this._transformStyleOptionConfig(styleOptionConfig);

    let defaultOption = deepClone(this._defaultOption);

    // 再合并自定义配置项
    if (defaultOption && option)
      defaultOption = mergeObject(defaultOption, option);

    //数据处理
    let series, count;

    if (field.length && value.length) {
      let fieldDescribe = field[0].fieldDescribe,
        fieldName = field[0].fieldName;

      series = dataList.reduce((r, c) => {
        let obj = {
          name: c[fieldName],
          [fieldDescribe]: c[fieldName],
          count: 0,
        };

        value.forEach((item) => {
          obj.count += c[item.fieldName];
          obj[item.fieldDescribe] = c[item.fieldName];
        });

        r.push(obj);
        return r;
      }, []);

      count = series.map((item) => item.count);
    }

    defaultOption.series = series || [];
    defaultOption.count = count || [];

    return defaultOption;
  }

  getClickData(event) {
    const { name, seriesName, value } = event;

    return {
      label: name,
      value: value[seriesName],
    };
  }
}
